<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');



require_once(APPPATH .'/libraries/REST_Controller.php');



/**
 * class Qualification
 * Will be the main controller for the vta application.
 *
 * @author      Jeroen Sen <jeroensen@22vision.nl>
 * @version     20121009
 *
 */
class Qualification extends REST_Controller {



    /**
     * public function all_get
     * curl -H "Accept: application/json" http://zichtopbomen.local/vta/qualification/all
     *
     * @author      Jeroen Sen <jeroensen@22vision.nl>
     * @version     20121009
     * @param
     * @return      void
     *
     */
    public function all_get()
    {

        $criteria = array();
        $orderBy = array('name' => 'ASC');

        $qualifications = $this->doctrine->em->getRepository('Entities\Qualification')->findBy($criteria, $orderBy);

        $data = array();

        foreach ($qualifications as $index => $qualification) {
            $data[] = array('id' => $qualification->getId(),
                            'name' => $qualification->getName(),
                            'description' => $qualification->getDescription(),
                            'scoremin' => $qualification->getScoremin(),
                            'scoremax' => $qualification->getScoreMax(),
                            'active' => $qualification->getActive(),
                            'name_score' => $qualification->getName() .' ('. $qualification->getScoremin() .'-'. $qualification->getScoremax() .')');
        }

        $this->response(array('data' => $data));

    } // public function stem($action = null)



    /**
     * public function create_put
     *
     * @author      Jeroen Sen <jeroensen@22vision.nl>
     * @version     20121009
     *
     */
    public function create_put()
    {
        $active = false;

        if ($this->put('active') == 'true') {
            $active = true;
        }

        $qualification = new Entities\Qualification;

        $qualification->setName($this->put('name'));
        $qualification->setDescription($this->put('description'));
        $qualification->setScoremin($this->put('scoremin'));
        $qualification->setScoremax($this->put('scoremax'));
        $qualification->setActive($active);

        $this->doctrine->em->persist($qualification);
		$this->doctrine->em->flush();

        $this->response('true');

    } // public function create_put()



    /**
     * public function update_post
     * curl -X POST -H "Accept: application/json" -d '{"id":666}' http://zichtopbomen.local/vta/qualification/edit
     *
     * @author      Jeroen Sen <jeroensen@22vision.nl>
     * @version     20121009
     *
     */
    public function update_post()
    {
        $id = intval($this->post('id'));
        $active = false;

        if ($this->post('active') == 'true') {
            $active = true;
        }


        $qualification = $this->doctrine->em->find('Entities\Qualification', $id);

        $qualification->setName($this->post('name'));
        $qualification->setDescription($this->post('description'));
        $qualification->setScoremin($this->post('scoremin'));
        $qualification->setScoremax($this->post('scoremax'));
        $qualification->setActive($active);

        $this->doctrine->em->persist($qualification);
		$this->doctrine->em->flush();

        $this->response('true');

    } // public function update_post()



    /**
     *
     *
     */
    public function test_get()
    {
        $this->response(array('data' => 'dit is een test'));

    } //public function test_get()




} // class Vta extends CI_Controller